﻿@namespace LaserMark.Components
@using LaserMark.DataAccess.Service

<div class="comprehensive">
    <ValidateForm Model="@Model" @ref="ValidateForm">
        <div class="row form-inline g-3">
            <div class="col-12">
                <Toggle @bind-Value="Model.IsOnStageStartDelay" OffText="脱离开关" OnText="传送延时" DisplayText="上片传送模式切换"></Toggle>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.OutPieceWaitDelay" DisplayText="出片等待延时"></BootstrapInputNumber>
                <label class="form-label ms-2">ms</label>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.TransStopDelay" DisplayText="传送停止延时"></BootstrapInputNumber>
                <label class="form-label ms-2">ms</label>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.TransUpDelay" DisplayText="传送带抬起延时"></BootstrapInputNumber>
                <label class="form-label ms-2">ms</label>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.TransDownDelay" DisplayText="传送带下降延时"></BootstrapInputNumber>
                <label class="form-label ms-2">ms</label>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.OnStageStartDelay" DisplayText="上片台启动延时"></BootstrapInputNumber>
                <label class="form-label ms-2">ms</label>
            </div>
            <div class="col-6">
                <BootstrapInputNumber @bind-Value="Model.OnStageStartTimeout" DisplayText="上片台超时时间"></BootstrapInputNumber>
                <label class="form-label ms-2">s</label>
            </div>
        </div>
    </ValidateForm>
</div>

<DialogFooter OnSave="@OnSave"></DialogFooter>


@code
{

    [NotNull]
    private ComprehensiveSetting? Model { get; set; } = new();

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Inject]
    [NotNull]
    private IComprehensiveService? Service { get; set; }

    [Inject]
    [NotNull]
    private ISecretService? SecretService { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        Model = await Service.Get();
    }

    private async Task<bool> OnSave(string password)
    {
        var ret = ValidateForm.Validate();
        var res = await SecretService.GetSecret("Customer", password);
        if (res)
        {
            await Service.Update(Model);
            return true;
        }
        else
        {
            return false;
        }
    }
}
